<template>
  <div class="goods">
    <!-- 面包屑导航区域 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>商品管理</el-breadcrumb-item>
      <el-breadcrumb-item>商品列表</el-breadcrumb-item>
    </el-breadcrumb>

    <!-- 卡片式图 -->
    <el-card>
      <!-- 搜索区域 添加商品区域 -->
      <el-row>
        <el-col :span="9">
          <el-input
            placeholder="请输入内容"
            v-model="queryInfo.query"
            clearable
            @clear="getGoods"
          >
            <el-button
              slot="append"
              icon="el-icon-search"
              @click="getGoods"
            ></el-button>
          </el-input>
        </el-col>
        <el-col :span="4">
          <el-button type="primary" @click="goAddPage">添加商品</el-button>
        </el-col>
      </el-row>

      <!-- 商品列表区域 -->
      <el-table
        :data="goodsList"
        style="width: 100%"
        border
        stripe
        height="660"
      >
        <el-table-column type="index" width="50"> </el-table-column>
        <el-table-column
          style="overflow: hidden; white-space: nowrap; text-overflow: ellipsis"
          label="商品名称"
          prop="goods_name"
          width="650"
        ></el-table-column>
        <el-table-column
          label="商品价格(元)"
          prop="goods_price"
          min-width="90"
        ></el-table-column>
        <el-table-column
          label="商品重量"
          prop="goods_weight"
          min-width="80"
        ></el-table-column>
        <el-table-column label="创建时间" prop="add_time" min-width="100">
          <template slot-scope="scope">
            {{ scope.row.add_time | dateFormat }}
          </template>
        </el-table-column>
        <el-table-column label="操作" min-width="120">
          <template slot-scope="scope">
            <el-button
              type="primary"
              icon="el-icon-edit"
              size="mini"
            ></el-button>
            <el-button
              type="danger"
              icon="el-icon-delete"
              size="mini"
              @click="delGood(scope.row.goods_id)"
            ></el-button>
          </template>
        </el-table-column>
      </el-table>

      <!-- 分页区域 -->
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="queryInfo.pagenum"
        :page-sizes="[5, 10, 15, 20]"
        :page-size="queryInfo.pagesize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      >
      </el-pagination>
    </el-card>
  </div>
</template>

<script>
export default {
  data() {
    return {
      total: 0, //商品总条数
      // 查询参数对象
      queryInfo: {
        query: "",
        pagenum: 1,
        pagesize: 10,
      },
      goodsList: [], //获取的商品列表数组
    };
  },
  mounted() {
    this.getGoods();
  },
  methods: {
    //获取所有商品和商品总数量
    getGoods() {
      this.axios
        .get("goods", {
          params: this.queryInfo,
        })
        .then((res) => {
          let result = res.data;
          console.log(result);
          if (result.meta.status !== 200) {
            return this.$message.error("获取商品列表失败");
          }
          this.goodsList = result.data.goods;
          this.total = result.data.total;
        });
    },
    // 分页部分的页数切换
    handleSizeChange(newSize) {
      this.queryInfo.pagesize = newSize;
      this.getGoods();
    },
    handleCurrentChange(newPage) {
      this.queryInfo.pagenum = newPage;
      this.getGoods();
    },
    //根据商品id删除对应商品
    delGood(goodId) {
      this.$confirm("此操作将永久删除该商品, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          this.axios.delete(`goods/${goodId}`).then((res) => {
            let result = res.data;
            if (result.meta.status !== 200) {
              return this.$message.error("删除商品失败");
            }
            this.getGoods();
            this.$message({
              type: "success",
              message: "删除成功!",
            });
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
    //跳转到添加商品页面
    goAddPage() {
      this.$router.push("/goods/add")
    },
  },
};
</script>

<style lang="scss" scoped>
.el-pagination {
  text-align: left;
}
</style>